﻿namespace GCC.Container.Abstracts
{
    /// <summary>
    /// 容器日志抽象
    /// </summary> 
    public abstract class AbsGCCLogger : IDisposable
    {
        /// <summary>
        /// 是否已经销毁
        /// </summary>
        protected bool _disposed = false;
        /// <summary>
        /// 模块名称
        /// </summary>
        protected readonly string _moduleName;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="moduleName">模块名称</param>
        public AbsGCCLogger(string moduleName)
        {
            _moduleName = moduleName;
        }

        /// <summary>
        /// 销毁函数
        /// </summary>
        /// <param name="flag">是否是用户主动调用</param>
        protected virtual void Dispose(bool flag)
        {
            if (_disposed)
            {
                return;
            }
            _disposed = true;
        }

        /// <summary>
        /// 销毁函数
        /// </summary> 
        public void Dispose()
        {
            Dispose(true);
        }


        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="level">级别</param>
        /// <param name="text">文本</param>
        /// <param name="eventID">事件ID</param>
        /// <param name="exception">异常</param>
        public abstract void Log(LogLevelEnum level, string text, string? eventID, Exception? exception);


        /// <summary>
        /// 获取完整的日志记录信息
        /// </summary>
        /// <param name="level">级别</param>
        /// <param name="text">文本</param>
        /// <param name="eventID">事件ID</param>
        /// <param name="exception">异常</param>
        protected string GetLogFullString(LogLevelEnum level, string text, string? eventID, Exception? exception)
        {
            return $"Local time:{DateTime.Now:yyyy-MM-dd HH:mm:ss} utc:{DateTime.Now.ToUniversalTime():yyyy-MM-dd HH:mm:ss} Module:{_moduleName} level:{Enum.GetName(typeof(LogLevelEnum), level)} eventID:{eventID ?? "Null"} ex:{(exception == null ? "Null" : exception.Message)} {text}";
        }


    }
}
